import { NzModalService } from 'ng-zorro-antd/modal';
import { AbsFormUtil } from '@abstract/form';
import { DatePipe } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { GriderList } from '@app/store/dict';
import { VisitService } from '@service/visit.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { VisitRecordDetailComponent } from './visit-record-detail/visit-record-detail.component';
import { AuthService } from '@service/auth.service';
import { VisitRecordResultComponent } from './visit-record-result/visit-record-result.component';
import { VisitRecordAddressComponent } from './visit-record-address/visit-record-address.component';
import { ActivatedRoute } from '@angular/router';

@Component({
	selector: 'app-visit-record',
	templateUrl: './visit-record.component.html',
	styleUrls: ['./visit-record.component.less'],
	providers: [DatePipe]
})
export class VisitRecordComponent extends AbsFormUtil implements OnInit {
	nzLoading = false;
	data = [];
	griderList = GriderList;
	status = '2';
	paramStatus: string;
	constructor(
		private visitService: VisitService,
		private nzMessageService: NzMessageService,
		private fb: FormBuilder,
		private datePipe: DatePipe,
		private nzModalService: NzModalService,
		private authService: AuthService,
		private route: ActivatedRoute,

	) {
		super();
		this.authList = this.authService.getAuth();
	}
	get isHandle() {
		// return true;
		return this.paramStatus === '1'
	}
	ngOnInit() {
		this.buildForm();
		this.route.params.subscribe(data => {
			this.paramStatus = data.id;
			this.onSearch(true);
			this.authList = this.authService.getAuth();
		});
	}

	buildForm() {
		this.validateForm = this.fb.group({
			griderType: [''],
			name: [''],
			beginDate: [''],
			endDate: [''],
		});
	}

	async onSearch(isPageInit?: boolean) {
		this.nzLoading = true;
		if (isPageInit) {
			this.page.page = 1;
		}
		const formValue = this.validateForm.value;
		if (formValue.beginDate) {
			formValue.beginDate = this.datePipe.transform(formValue.beginDate, 'yyyy-MM-dd');
		}
		if (formValue.endDate) {
			formValue.endDate = this.datePipe.transform(formValue.endDate, 'yyyy-MM-dd');
		}
		const params = { ...formValue, ...this.page, status: this.paramStatus};
		const result = await this.visitService.getGriderVisit(params);
		this.data = result.data.list;
		this.page.total = result.data.totalCount;
		this.nzLoading = false;

	}


	async handle(component, item) {
		const {id,treatResult } = item;
		if (!treatResult) {
			this.nzMessageService.error('处理结果不能为空');
			return false;
		}
		component.hide();
		await this.visitService.saveVisit({visitId: id, treatResult});
		this.nzMessageService.success('处理成功');
		this.onSearch();
	}
	async verify(component, item) {
		const {id, verifyResult, treatId } = item;
		if (!verifyResult && this.status === '2') {
			this.nzMessageService.error('审核内容不能为空');
			return false;
		}
		component.hide();
		await this.visitService.updateVisit({visitId: id, verifyResult:  verifyResult ? verifyResult : '审核通过', id: treatId, status: this.status});
		this.nzMessageService.success('审核成功');
		this.onSearch();
	}
	detail(item) {
		this.nzModalService.create({
			nzTitle: '走访详情',
			nzContent: VisitRecordDetailComponent,
			nzWidth: '60%',
			nzOkText: null,
			nzComponentParams:{
				item: item
			}
		});
	}
	result(item) {
		this.nzModalService.create({
			nzTitle: '处理结果',
			nzContent: VisitRecordResultComponent,
			nzWidth: '70%',
			nzOkText: null,
			nzComponentParams:{
				id: item?.id
			}
		});
	}

	address(item) {
		this.nzModalService.create({
			nzTitle: '村户详情',
			nzContent: VisitRecordAddressComponent,
			nzWidth: '50%',
			nzOkText: null,
			nzComponentParams:{
				item: item
			}
		});
	}
}
